<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);
$item_id = MiscUtils::getParam('id', 0);
$parent_id = MiscUtils::getParam('pid', 0);

$item = NULL;
$cond_vals = new stdClass();
$cond_vals->c = 't.id = :v1';
$cond_vals->v = array(':v1' => $item_id);
$tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'art_tag', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
if (count($tmp) > 0) {
  $item = $tmp[0];
}

$categories = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'art_tag', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
table.form {
  background-color: #fff;
  border-top: 1px solid #222;
  border-left: 1px solid #222;
}
table.form th.header {
  text-align: left;
  color: #fff;
  background-color: #222;
  padding-left: 5px;
  font-weight: bold;
  height: 25px;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
}

table.form td {
  padding: 3px 5px; 
  vertical-align: middle;
}
table.form td.title {
  background-color: #666;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
  width: 150px;
  color: #fff;
}
table.form td.content {
  width: 300px;
  padding: 0;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
}
table.form td.content input,select {
  width: 98%;
  height: 20px;
  border: 0;
  padding-left: 2px;
}
.imgbtn {
  background-image: url(assets/imgs/save.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
}
.imgbtn:hover {
  background-color: #eee;
}
ul.hztl li {
  display: inline;
  vertical-align: top;
}
</style>
</head>
<body>
<div>
  <table class="form" cellpadding=0 cellspacing=0>
    <tr><th class="header" colspan=2>Edit Tag</th></tr>
    <tr>
      <td class="title">Tag Name</td>
      <td class="content"><input value="<?php echo (($item) ? $item->name : ''); ?>" id="name" type="text"></input></td>
    </tr>
    <tr>
      <td class="title">Parent</td>
      <td class="content"><select id="parent_select"></select></td>
    </tr>
  </table>
  <button class="imgbtn" onclick="save();" style="margin-top: 5px;">Save</button>
  <div style="height: 200px"></div>
</div>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.queue.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/myupload2.js"></script>
<script type="text/javascript" src="../../../incls/js/pageutils.js"></script>
<script type="text/javascript">
  $(function(){
    set_parent(PageUtils.getRoot(JSON.parse('<?php echo addslashes(json_encode($categories)); ?>')), $('#parent_select')[0], '', true);
    
    //NOT GOOD
    var div = parent.document.getElementById('<?php echo LAST_NAV; ?>');
    var div_parent = div.parentNode;
    var value = div_parent.firstChild.nodeValue;
    MyUtils.removeChildElements(div_parent);
    var elm;
    elm = parent.document.createElement('a');
    elm.href = '#';
    elm.style.color = '#000';
    elm.style.fontSize = '13px';
    elm.style.fontWeight = 'bold';
    elm.onclick = function() {
      parent.location.href = 'http://<?php echo URL . 'admin.php?i=' . $page_id; ?>';
    };
    elm.appendChild(parent.document.createTextNode(value));
    div_parent.appendChild(elm);

    var td;
    td = div_parent.parentNode.insertCell(-1);
    td.style.width = '20px';
    td.style.textAlign = 'center';
    td.style.color = '#aaa';
    td.appendChild(parent.document.createTextNode('>'));
    
    td = div_parent.parentNode.insertCell(-1);
    td.style.height = '25px';
    td.style.color = '#333';
    td.style.fontSize = '13px';
    <?php if ($item) { ?> 
    td.title = '<?php echo $item->name; ?>';
    td.appendChild(parent.document.createTextNode(MyUtils.getShort('<?php echo $item->name; ?>', 10)));
    <?php } else { ?>
    td.appendChild(parent.document.createTextNode('New Tag'));
    <?php } ?>
    //NOT GOOD END
  });
  
  function save() {
    var obj = {
      <?php if ($item) { ?>
      id: '<?php echo $item->id; ?>',
      <?php } ?>
      parent_id: $('#parent_select')[0].options[$('#parent_select')[0].selectedIndex].value,
      name: document.getElementById('name').value
    }
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: "action=save&q=" + encodeURIComponent(JSON.stringify(obj)),
      success: function(msg){
        var obj = JSON.parse(msg);
        parent.location.href = 'http://<?php echo URL . "admin.php?i=$page_id&script=index_tag&id="; ?>' + obj.id;
      }
    });
  }

  function set_parent(node, elm, prefix, selectable) {
    node.title = (node.name) ? node.name : node.title;
    var opt = new Option(((prefix == '') ? '' : prefix + ') ' ) + ((node.id != 0 && node.title == '') ? 'Untitled' : node.title), node.id);
    if (node.id == '<?php echo isset($item) ? $item->id : -1; ?>' || !selectable) {
      opt.disabled = true;
    }
    elm.options[elm.options.length] = opt;
    if ('<?php echo $parent_id; ?>' == node.id) {
      elm.selectedIndex = elm.options.length - 1;
    }
    if (node.id == '<?php echo isset($item) ? $item->parent_id : -1; ?>') {
      elm.selectedIndex = elm.options.length - 1;
    }
    for (var idx in node._children) {
      var obj = node._children[idx];
      set_parent(obj, elm, ((prefix == '') ? '(' : '\u2002' + prefix + '-' ) + (parseInt(idx, 10) + 1), !opt.disabled);
    }
  }
</script>
</body>
</html>